package com.example.mystore.servlets.service;

import com.example.mystore.dao.CustomersDao;
import com.example.mystore.entity.Customer;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {

     public void doGet(HttpServletRequest req, HttpServletResponse resp){
        String contextPath = req.getContextPath();
        String url= "/login.jsp";
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        Customer customer = CustomersDao.selectCustomer(email,password);
        if (customer != null){
//            保存登录顾客状态
            req.getSession().setAttribute("customer",customer);
//            移除错误信息
            req.getSession().removeAttribute("err");
            url= "/display.jsp";
        }else {
//            登录页面提示错误信息
            req.getSession().setAttribute("err","您输入的邮箱地址或密码错误");
        }
         try {
             resp.sendRedirect(contextPath+url);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

    public void doPost(HttpServletRequest req, HttpServletResponse resp){
        doGet(req,resp);
    }
}
